package com.eapi.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eapi.eapicommom.model.entity.InterfaceInfo;
import com.eapi.eapicommom.model.entity.User;
import com.eapi.project.annotation.AuthCheck;
import com.eapi.project.common.BaseResponse;
import com.eapi.project.common.ErrorCode;
import com.eapi.project.common.ResultUtils;
import com.eapi.project.exception.BusinessException;
import com.eapi.project.model.dto.article.*;
import com.eapi.project.model.entity.Article;
import com.eapi.project.model.vo.*;
import com.eapi.project.service.ArticleService;
import com.eapi.project.service.ArticlelikerecordService;
import com.eapi.project.service.InterfaceInfoService;
import com.eapi.project.service.UserService;
import com.eapi.project.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Resource
    private UserService userService;

    @Autowired
    private ArticlelikerecordService articlelikerecordService;

    @Resource
    private InterfaceInfoService interfaceInfoService;

    @GetMapping("/hotArticleList")
    public BaseResponse<List<HotArticleVo>> hotArticleList() {
        return articleService.hotArticleList();
    }

    @GetMapping("/articleList")
    public BaseResponse<PageVo> articleList(Integer pageNum, Integer pageSize, Long categoryId) {
        return articleService.articleList(pageNum, pageSize, categoryId);
    }

    @GetMapping("/getArticleDetail")
    public BaseResponse<ArticleDetailVo> getArticleDetail(Long id) {
        return articleService.getArticleDetail(id);
    }

    @PostMapping("/addArticle")
    public BaseResponse<String> addArticle(@RequestBody AddArticleDto addArticleDto, HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        articleService.addArticle(addArticleDto, userId);
        return new BaseResponse<>(0, "添加成功");
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteArticle(@RequestBody ArticleDeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        return articleService.deleteArticle(deleteRequest, user, request);
    }


    // 修改自己的文章
    @PostMapping("/update")
    public BaseResponse<Boolean> updateArticle(@RequestBody ArticleUpdateRequest articleUpdateRequest,
                                               HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return articleService.updateArticle(articleUpdateRequest, user);
    }

    // 管理员获取所有文章（包括草稿）
    @AuthCheck(mustRole = "admin")
    @GetMapping("/listAllArticle")
    public BaseResponse<PageVo> listAllArticle(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return articleService.listAllArticle(pageNum, pageSize, user);
    }

    // 管理员对文章进行置顶
    @AuthCheck(mustRole = "admin")
    @PostMapping("/top")
    public BaseResponse<String> topArticle(@RequestBody ArticleTopRequest articleTopRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return articleService.topArticle(articleTopRequest, user);
    }

    // 取消置顶
    @AuthCheck(mustRole = "admin")
    @PostMapping("/cancelTop")
    public BaseResponse<String> cancelTop(@RequestBody ArticleTopRequest articleTopRequest, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return articleService.cancelTopArticle(articleTopRequest, user);
    }

    @PostMapping("/like")
    public BaseResponse<String> likeArticle(@RequestBody ArticleLikeRequest articleLikeRequest, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        return articlelikerecordService.likeArticle(articleLikeRequest,loginUser);
    }

    /**
     * 获取草稿
     */
    @GetMapping("/getDraft")
    public BaseResponse<PageVo> getDraft(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return articleService.getDraft(user, pageNum, pageSize);
    }

    /**
     * 查询已发布的文章
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @GetMapping("/getPublic")
    public BaseResponse<PageVo> getPublic(Integer pageNum, Integer pageSize, HttpServletRequest request) {
        User user = userService.getLoginUser(request);
        return articleService.getPublic(user, pageNum, pageSize);
    }

    /**
     * 获取对方作者信息
     */
    @GetMapping("/getAuthorInfo")
    public BaseResponse<AuthorVO> getAuthorInfo(Long userId) {
        User user = userService.getById(userId);
        AuthorVO authorVO = new AuthorVO();
        BeanUtils.copyProperties(user, authorVO);
        // 查询该用户发表的文章
        LambdaQueryWrapper<Article> articleQueryWrapper = new LambdaQueryWrapper<>();
        articleQueryWrapper.eq(Article::getCreateBy, userId);
        articleQueryWrapper.eq(Article::getStatus,0);
        articleQueryWrapper.orderByDesc(Article::getCreateTime);
        List<Article> articleList = articleService.list(articleQueryWrapper);
        List<AuthorArticleListVo> authorArticleListVos = BeanCopyUtils.copyBeanList(articleList, AuthorArticleListVo.class);
        authorVO.setArticleList(authorArticleListVos);
        // 查询该用户发表过的接口
        List<InterfaceInfo> interfaceInfoList = interfaceInfoService.list(new LambdaQueryWrapper<InterfaceInfo>().eq(InterfaceInfo::getUserId, userId));
        List<AuthorInterfaceInfoVo> authorInterfaceInfoVos = BeanCopyUtils.copyBeanList(interfaceInfoList, AuthorInterfaceInfoVo.class);
        authorVO.setInterfaceInfoList(authorInterfaceInfoVos);
        return ResultUtils.success(authorVO);
    }
}